home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / STDLIB.PAK / CALC.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  2KB  |  94 lines

  1. /**************************************************************************
  2.  *
  3.  * calc.cpp - RPN Calculator -- Illustration of the use of stacks.
  4.  *      Section 10.2.1
  5.  *
  6.  * $Id: calc.cpp,v 1.3 1995/08/29 19:00:41 oberg Exp $
  7.  *
  8.  * $$RW_INSERT_HEADER "slyrs.cpp"
  9.  *
  10.  **************************************************************************/
  11.  
  12. # include <vector>
  13. # include <stack>
  14. # include <iostream.h>
  15. using namespace std;
  16.  
  17. //
  18. //    class calculatorEngine
  19. //        simulate the behavior of a simple integer calculator
  20. //
  21.  
  22. class calculatorEngine {
  23. public:
  24.     enum binaryOperator {plus, minus, times, divide};
  25.     
  26.     int currentMemory ()
  27.         { return data.top(); }
  28.         
  29.     void pushOperand (int value)
  30.         { data.push (value); }
  31.         
  32.     void doOperator (binaryOperator);
  33.     
  34. protected:
  35.     stack< int, vector<int> > data;
  36. };
  37.  
  38. void calculatorEngine::doOperator (binaryOperator theOp)
  39. {
  40.     int right = data.top();
  41.     data.pop();
  42.     int left = data.top();
  43.     data.pop();
  44.     switch (theOp) {
  45.         case plus: data.push(left + right); break;
  46.         case minus: data.push(left - right); break;
  47.         case times: data.push(left * right); break;
  48.         case divide: data.push(left / right); break;
  49.         }
  50. }
  51.  
  52. int main() {
  53.     cout << "Calculator example program, from Chapter 8" << endl;
  54.     cout << "Enter a legal RPN expression, end with p q (print and quit)" << endl;
  55.     int intval;
  56.     calculatorEngine calc;
  57.     char c;
  58.     
  59.     while (cin >> c)
  60.         switch (c) {
  61.             case '0': case '1': case '2': case '3': case '4':
  62.             case '5': case '6': case '7': case '8': case '9':
  63.                 cin.putback(c);
  64.                 cin >> intval;
  65.                 calc.pushOperand(intval);
  66.                 break;
  67.             
  68.             case '+':
  69.                 calc.doOperator(calculatorEngine::plus);
  70.                 break;
  71.                         
  72.             case '-':
  73.                 calc.doOperator(calculatorEngine::minus);
  74.                 break;
  75.                 
  76.             case '*':
  77.                 calc.doOperator(calculatorEngine::times);
  78.                 break;
  79.                 
  80.             case '/':
  81.                 calc.doOperator(calculatorEngine::divide);
  82.                 break;
  83.  
  84.             case 'p':
  85.                 cout << calc.currentMemory() << endl;
  86.                 
  87.             case 'q':
  88.                 cout << "End calculator program" << endl;
  89.                 return 0; // quit program
  90.  
  91.         }
  92.     return 0;
  93. }
  94.